<template>
    <div>
        <Form :label-width="100" inline>
            <Form-item label="流程配置:">
                <dictSelect :sel_val='queryParam.processid' dicType="service" :bindData.sync="queryParam.processid"  :serviceParam="{}" style="width: 300px"
                            dictService="/api/ac/subsistence/extActConfigService/getProcessDictList" class="itemStyle"   />
            </Form-item>
            <Form-item label="救助类别:">
                <dictSelect :sel_val='queryParam.assistancetype' dictCode="assistancetype" style="width: 180px"
                            :bindData.sync="queryParam.assistancetype" clearable showKeys="100,101,102,200,201,202,203,500"/>
            </Form-item>
            <Form-item label="审批类型:">
                <dictSelect :sel_val='queryParam.handletype' dictCode="handletype" style="width: 180px"
                            :bindData.sync="queryParam.handletype" clearable/>
            </Form-item>
            <Row class="button_row">
                <span style="float: left;">
                <Button type="primary" @click="handleAdd()">录入</Button>
                </span>
                <span style="float: right;">
                      <Button type="primary" @click="searchQuery">查询</Button>
                <Button type="primary" class="button_item_left" @click="searchReset">重置</Button>
                </span>
            </Row>
        </Form>
        <Modal v-model="visible" :title="title" width="650" style="position:relative;">
            <Form ref="formData" :model="formData" :label-width="150" :rules="rules" :disabled="disableSubmit">
                <FormItem label="救助类别" prop="assistancetype">
                    <dictSelect :sel_val='formData.assistancetype' dictCode="assistancetype"
                                @changeState="changeAssistanceType"
                                :bindData.sync="formData.assistancetype" clearable style="width: 300px"/>
                </FormItem>
                <br>
                <FormItem label="审批类型" prop="handletype">
                    <dictSelect :sel_val='formData.handletype' dictCode="handletype" @changeState="changeHandleType"
                                :bindData.sync="formData.handletype" clearable style="width: 300px"/>
                </FormItem>
                <br>
                <FormItem label="行政区划">
                    <area-linkage :areaCode.sync="formData.areacode" :countyDisable=false :streetDisable=false
                                  :communityDisable=false></area-linkage>
                </FormItem>
                <br>
                <FormItem label="是否沈阳市">
                    <c-dict-select v-model="formData.isCityOrNot" dictCode="yesorno" :width="300"  @change="changeIsCityOrNot"/>
                </FormItem>
                <br>
                <FormItem label="流程配置" prop="processid">
                    <Select v-model="formData.processid" style="width: 300px">
                        <Option v-for="item in processData" :value="item.id" :key="item.id">{{item.processname}}
                        </Option>
                    </Select>
                </FormItem>
            </Form>
            <div slot="footer">
                <Button type="text" size="large" @click="visible = false">取消</Button>
                <template v-if="!disableSubmit">
                    <Button type="primary" size="large" @click="handleSubmit('formData')">确定</Button>
                </template>
            </div>
        </Modal>
        <div class="table_div">
            <Table :columns="columns" :data="tableData" :loading="Loading" border>
                <template slot-scope="{ row, record, index  }" slot="action">
                    <Button type="info" size="small" style="margin-right: 5px" @click="handleEdit(index)">编辑</Button>
                    <Button type="error" size="small" style="margin-right: 5px" @click="handleDelete(row.id)">删除
                    </Button>
                    <Button type="success" size="small" style="margin-right: 5px" @click="handleDetail(index)">详情
                    </Button>
                </template>
            </Table>
            <Page :total="dataCount" :page-size="pageSize" show-total class="paging"
                  @on-change="handleTableChange" :current="pageNo"></Page>
        </div>
        <!--        <rule-action-infor-modal ref="modalForm" @ok="modalFormOk"/>-->
    </div>
</template>

<script>
    import {postAction} from '@/api/request'
    import {CommonMixin} from '@/views/common/js/CommonMixin'
    import dictSelect from '@/components/dictSelect/dictSelect.vue'

    export default {
        mixins: [CommonMixin],
        components: {
            dictSelect,
        },
        watch: {
            "formData.areacode": function (val, oldval) {
                let that = this;
                if (val) {
                    // that.formData.isCityOrNot = undefined;
                    that.$delete(that.formData,'isCityOrNot')
                }
            },
        },
        data() {
            return {
                rules: {
                    processid: [
                        {required: true, message: '请选择"流程配置"', trigger: 'blur'}
                    ],
                    areacode: [
                        {required: true, message: '请选择"行政区划"', trigger: 'blur'}
                    ],
                    handletype: [
                        {required: true, message: '请选择"审批类型"', trigger: 'blur'}
                    ],
                    assistancetype: [
                        {required: true, message: '请选择"救助类别"', trigger: 'blur'}
                    ],
                },
                fieldsToValidate: ["assistancetype", "handletype", "processid"],
                columns: [
                    {
                        title: '流程配置',
                        align: 'center',
                        key: 'processname'
                    },
                    {
                        title: '救助类别',
                        align: 'center',
                        key: 'assistancetype_name'
                    },
                    {
                        title: '审批类型',
                        align: 'center',
                        key: 'handletype_name'
                    },
                    {
                        title: '行政区划',
                        align: 'center',
                        key: 'areacode_name'
                    },
                    {
                        title: '操作',
                        key: 'action',
                        align: 'center',
                        slot: 'action',
                        width: '200px'
                    }
                ],
                tableData: [],
                Loading: false,
                url: {
                    list: "/api/ac/subsistence/extActConfigService/getPageList",
                    delete: "/api/ac/subsistence/extActConfigService/deleteData"
                },
                formData: {},
                processData: {},
                queryParam: {},
                visible: false,
                dataCount: 0,
                pageSize: 10,
                modal: false,
                title: "",
                disableSubmit: false
            }
        },
        methods: {
            handleAdd() {
                this.$refs['formData'].resetFields();
                this.disableSubmit = false;
                let that = this;
                that.title = "区划流程添加"
                let record = {};
                that.visible = true
                that.formData = Object.assign({}, record)
            },
            handleDetail(index) {
                this.$refs['formData'].resetFields();
                let that = this;
                that.title = "详情";
                that.disableSubmit = true;
                let record = that.tableData[index];
                if (record.id) {
                    that.visible = true
                    that.formData = Object.assign({}, record);
                    that.queryProcess();
                } else {
                    that.visible = false
                }
            },
            handleEdit(index) {
                this.$refs['formData'].resetFields();
                this.disableSubmit = false;
                let that = this;
                that.title = "区划流程编辑"
                let record = that.tableData[index];
                if (record.id) {
                    that.visible = true
                    that.formData = Object.assign({}, record)
                    that.queryProcess();
                } else {
                    that.visible = false
                }
            },
            handleSubmit(name) {
                let that = this;
                let validate = true;
                let err_msg = [];
                for (let item of that.fieldsToValidate) {
                    that.$refs[name].validateField(item, (error) => {
                        if (error) {
                            validate = false;
                            err_msg.push(that.rules[item][0].message);
                        }
                    })
                }
                if (validate) {
                    let that = this;
                    that.Loading = true;
                    let url = "/api/ac/subsistence/extActConfigService/insertData";
                    if (this.formData.id) {
                        url = "/api/ac/subsistence/extActConfigService/updateData";
                    }
                    if (!that.formData.areacode) {
                        if (!that.formData.isCityOrNot || that.formData.isCityOrNot == '0') {
                            that.$Message.error("请选择区划");
                            return;
                        } else {
                            that.formData.areacode = "210100000000";
                        }
                    }
                    postAction(url, this.formData).then((res) => {
                        if (res.data.errcode != 0) {
                            this.$Message.error(res.data.errmsg);
                        } else {
                            this.modalFormOk();
                            this.visible = false;
                        }
                    }).finally(() => {
                        that.Loading = false;
                    });
                } else {
                    that.$Message.error(err_msg[0]);
                    return;
                }
            },
            //根据 救助类别,审批类型, 选择流程配置
            changeAssistanceType() {
                this.queryProcess();
            },
            changeHandleType() {
                this.queryProcess();
            },
            queryProcess() {
                let that = this;
                if (that.formData.assistancetype != "" && that.formData.assistancetype != undefined &&
                    that.formData.handletype != "" && that.formData.handletype != undefined) {
                    let url = "/api/ac/subsistence/extActConfigService/getProcessList";
                    postAction(url, that.formData).then((res) => {
                        if (res.data.errcode != 0) {
                            this.$Message.error(res.data.errmsg);
                        } else {
                            that.processData = res.data.data
                        }
                    }).finally(() => {

                    });
                }
                let url1 = "/api/ac/subsistence/extActConfigService/getXZQH";
                postAction(url1, that.formData).then((res) => {
                    if (res.data.errcode != 0) {
                        this.$Message.error(res.data.errmsg);
                    } else {

                    }
                }).finally(() => {
                });
            },

            changeIsCityOrNot() {
                let that = this;
                if (that.formData.isCityOrNot == '1') {
                    that.formData.areacode = undefined;
                }
            }
        },
        created() {
            this.loadData();
        },
    }
</script>
<style scoped>
    /*.paging {*/
    /*    float: right;*/
    /*    margin-top: 10px;*/
    /*}*/

    /*/deep/ .ivu-upload-list {*/
    /*    display: none;*/
    /*}*/
    /deep/ .ivu-form-item-error-tip {
        display: none;
    }

    .ivu-form-item {
        margin-bottom: 0px;
    }

    .itemStyle {
        width: 180px
    }

    .head-img1 {
        height: 200px;
        width: 250px;
        margin-top: 15px;
    }

    .head-img {
        height: 200px;
        width: 250px;
    }

    .imgBackColor {
        background-color: WhiteSmoke;
        height: 250px;
    }

    .positiveTDiv {
        height: 240px;
        width: 75px;
        line-height: 240px;
        background-color: #7CCD7C;
        margin-bottom: 10px;
        color: white;
        font-size: 14px;
        font-weight: 700;
    }
</style>
